﻿using UnityEngine;

using System.Collections;



public class startTouchController : MonoBehaviour
{



    // Values to set:

    public float dragThreshold = 100.0f;

    public float minSwipeDist = 0.0f;

    public float maxSwipeTime = 0.5f;
	
	public float directionalThreshold = 20.0f;


    private float startTime;

    private Vector2 startPos;

    private bool couldBeSwipe;



    public enum SwipeDirection
    {

        None,

        Up,

        Down,
		
		Left,
		
		Right

    }



    public static SwipeDirection lastSwipe = SwipeDirection.None;

    public float lastSwipeTime;



    void Update()
    {
		//lastSwipe = SwipeDirection.None;
        if (Input.touchCount > 0)
        {

            
			            int fingerCount = 0;
            foreach (Touch touches in Input.touches)
            {
                if (touches.phase != TouchPhase.Ended && touches.phase != TouchPhase.Canceled)
                    fingerCount++;
            }
			Touch touch = Input.touches[0];

            switch (touch.phase)
            {

                case TouchPhase.Began:

                    lastSwipe = SwipeDirection.None;

                    lastSwipeTime = 0;

                    couldBeSwipe = true;

                    startPos = touch.position;

                    startTime = Time.time;

                    break;



                case TouchPhase.Moved:

//                    if (Mathf.Abs(touch.position.x - startPos.x) > dragThreshold)
//                    {
//
//                        Debug.Log("Not a swipe. Swipe strayed " + (int)Mathf.Abs(touch.position.x - startPos.x) +
//
//                                  "px which is " + (int)(Mathf.Abs(touch.position.x - startPos.x) - dragThreshold) +
//
//                                  "px outside the comfort zone.");
//
//                        couldBeSwipe = false;
//
//                    }
				
                    break;

                case TouchPhase.Ended:

                    if (couldBeSwipe)
                    {

                        float swipeTime = Time.time - startTime;

                        float swipeDistx = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
					    float swipeDisty = (new Vector3(touch.position.x, 0, 0) - new Vector3(touch.position.x, 0, 0)).magnitude;
	 					//print("x:"+swipeDistx+",y:"+swipeDisty+" time:"+swipeTime);	                   
	 					//print("x:"+touch.deltaPosition.x+",y:"+touch.deltaPosition.y+" time:"+swipeTime);	                   
                        if(true) //((swipeTime < maxSwipeTime) && (Mathf.Abs(swipeDistx) > minSwipeDist || Mathf.Abs(swipeDisty) > minSwipeDist))
                        {

                            // It's a swiiiiiiiiiiiipe!

                            float swipeValue = (touch.position.y - startPos.y);
						    float swipeValuex = (touch.position.x - startPos.x);
							print(swipeValuex+" ,"+swipeValue);

                            // If the swipe direction is positive, it was an upward swipe.

                            // If the swipe direction is negative, it was a downward swipe.

                            if (swipeValuex < -5 && Mathf.Abs(swipeValue) < directionalThreshold)

                                lastSwipe = SwipeDirection.Left;

                         	else if (swipeValuex > 5 && Mathf.Abs(swipeValue) < directionalThreshold)

                                lastSwipe = SwipeDirection.Right;
						
							if (swipeValue > 5 && Mathf.Abs(swipeValuex) < directionalThreshold)
								lastSwipe = SwipeDirection.Up;
							
							else if (swipeValue < -5 && Mathf.Abs(swipeValuex) < directionalThreshold)
								lastSwipe = SwipeDirection.Down;
							


                            // Set the time the last swipe occured, useful for other scripts to check:

                            lastSwipeTime = Time.time;

                            Debug.Log("Found a swipe!  Direction: " + lastSwipe);

                        }

                    }

                    break;

            }

        }

    }

}   

